package org.crh.share;

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 初始化加载参数
 */
@Component
public class InitDataToRedis {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    RedissonClient redissonClient;

    @PostConstruct
    public void init() {
        // 获取所有科室ID
        List<Long> scheduleIds = scheduleService.getAllIds();
        scheduleIds.forEach(id -> {
            initStockToRedis(id);
        });
    }

    private void initStockToRedis(Long slotId) {
        // 从数据库加载库存
        Integer dbStock = scheduleService.selectStock(slotId);
        String key =String.valueOf(slotId);
        RBucket<String> bucket = redissonClient.getBucket(key, StringCodec.INSTANCE);
        bucket.set(String.valueOf(dbStock));
    }
}
